import {
  api,
  util,
  storage
} from '../util'
import authorize from './authorize.js'
const app = getApp();
const getPicVerifyCode = (fn) => {
  var content = {
    codeBase64: "",
    codeToken: ""
  };
  util.get(api.host + "/VerifyCode.axd?base64=1",
    function(res) {
      var codeToken = res.header['codeToken'] || res.header['codetoken'];
      content = {
        codeBase64: res.data,
        codeToken: codeToken
      };
      storage.put("pic_verify_code", codeToken, 120);
      if (fn && typeof(fn) == "function") {
        fn(content);
      }
    });
}

const sms = {
  notifyTime: 60,
  /*获取短信校验码 */
  getSmsVerifyCode: function(param, method, callback) {
    util.post(method, param, function(res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof(callback) == "function") {
          callback(res.data);
        }
      } else {
        util.alert(res.Message);
      }
    },"3.1");
  },
  /**
    * 校验短信
    */
  validate_sms: (method,mobile, code, callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Mobile: mobile,
      ValidateCode: code
    };
    util.post(method, param,
      function (res) {
        res = res.data;

        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.alert(res.Message);
        }

      },"3.0", null, false);
  }

}
/**
 * 信息相关
 */
const info = {
  loadList: (page, callback) => {
    var entity = authorize.getEntity();
    var param = util.mergeJsonObject(page, {
      Token: entity.manager.Token,
      ReceiverId: entity.fkId,
      ReceiverFlag: entity.fkFlag
    });
    util.post("info.info.list", param, function(res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof(callback) == "function") {
          callback(res.Content);
        }
      } else {
        util.alert(res.Content);
      }
    },"3.1");
  },
  /**
   * 详情
   */
  detail: (id, callback) => {
    var entity = authorize.getEntity();
    var param = {
      Id: id
    };
    util.post("info.info.detail", param, function(res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof(callback) == "function") {
          callback(res.Content[0]);
        }
      } else {
        util.alert(res.Content);
      }
    });
  },
  /**
   * 标记为全部已读
   */
  read_all: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      Terminal: "MiniProgram"
    };
    util.fetch("info.info.readall", param, callback);
  },
  /**
   * 删除所有
   */
  del_all: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag
    };
    util.fetch("info.info.deleteall", param, callback);
  },
  /**
   * 标记为已读
   */
  read: (id, callback) => {
    var entity = authorize.getEntity();
    var param = {
      Id: id,
      Terminal: "MiniProgram"
    };
    util.fetch("info.info.read", param, callback);
  },
  /**
   * 是否有新消息
   * 返回Int >0有新消息
   */
  hasNewMessage(callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      ManagerId: entity.manager.ManagerId,
      RelateType: 'b2corder',
      proprietor:app.dns.proprietor,
      proprietorId: app.dns.proprietorId,
      Token:entity.manager.Token
    };
    util.post("info.info.hasnewmsg", param, function(res) {
      if (callback != undefined && typeof(callback) == "function") {
        callback(res.data.Content);
      }
    },"3.1",null,false);
  }
}

export {
  getPicVerifyCode,
  sms,
  info
}